// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================
package tc.map.test;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import tc.map.TcMview;
import tc.map.TcMviewControl;
import tc.map.TcTileAddress;

//=============================================================================
/**
Toolbar for application for testing TcMview - the Open Street Map tiled map viewer.
Enables map source selection (the first combo), contains next and previous layer selection
and informs about active map layer (the number between the buttons).
*/
public class TcMapToolbar extends JPanel implements ActionListener {
//=============================================================================
private TcMviewControl mapControl;
	
private JButton button_zoomout;
private JLabel label_current_level;
private JButton button_zoomin;

//=============================================================================
public TcMapToolbar(TcMview mapView) {
	this.mapControl = mapView.getController();
	
	setLayout(new FlowLayout(FlowLayout.LEFT));
	
	add(new JLabel(" Tile Source: "));
	
	add(new JComboBox(new TcMviewSource()) );
	
	try {
		ImageIcon iconOut = new ImageIcon(getClass().getResource("images/zoomout.png"));
		button_zoomout = new JButton("???", iconOut);
	}
	catch (Exception e) {
		button_zoomout = new JButton("???");
	}
	button_zoomout.setActionCommand("zoomout");
	button_zoomout.addActionListener(this);
	add(button_zoomout);

	add(new JLabel(" Current Layer: "));

	label_current_level = new JLabel("???");
	label_current_level.setFont(new Font(null, Font.BOLD, 28));
	add(label_current_level);

	try {
		ImageIcon iconIn = new ImageIcon(getClass().getResource("images/zoomin.png"));
		button_zoomin = new JButton("???", iconIn);
	}
	catch (Exception e) {
		button_zoomin = new JButton("???");
	}
	button_zoomin.setActionCommand("zoomin");
	button_zoomin.addActionListener(this);
	add(button_zoomin);
	
	add(new JLabel("   Layers are 1-18. Enter two left mouse clicks to move the map."));
	
	updateInfo(TcTileAddress.LAYER_MIN);
}

@Override
public void actionPerformed(ActionEvent e) {
	if ("zoomin".equals(e.getActionCommand())) {
		mapControl.zoomin();
	} else if ("zoomout".equals(e.getActionCommand())) {
		mapControl.zoomout();
	}
	
	updateInfo(mapControl.getCurrentLayer());
}

/**Changes the toolbar information according to the actual layer
 */
private void updateInfo (int layer) {
	label_current_level.setText(String.format("%d ", layer));
	if (layer == TcTileAddress.LAYER_MIN) {
		button_zoomout.setText("Disabled");
		button_zoomout.setEnabled(false);
	}
	else {
		button_zoomout.setText(String.format("Layer %d", layer-1));
		button_zoomout.setEnabled(true);
	}
	if (layer == TcTileAddress.LAYER_MAX) {
		button_zoomin.setText("Disabled");
		button_zoomin.setEnabled(false);
	}
	else {
		button_zoomin.setText(String.format("Layer %d", layer+1));
		button_zoomin.setEnabled(true);
	}
}

//=============================================================================
/**Model for the map source combo.
 */
public class TcMviewSource extends DefaultComboBoxModel {
	public TcMviewSource () {
		addElement(new TcMviewSourceItem("Standard", "http://tile.openstreetmap.org/"));
		addElement(new TcMviewSourceItem("Cycle", "http://b.tile.opencyclemap.org/cycle/"));
		addElement(new TcMviewSourceItem("Freeway", "http://b.tah.openstreetmap.org/Tiles/tile/"));
	}
	
	public void setSelectedItem(Object item)
	{	TcMviewSourceItem itemEx = (TcMviewSourceItem)item;
		TcMviewControl.httpbase = itemEx.url;
		super.setSelectedItem(item);
	}
}

//=============================================================================
/**Model item for the map source combo.
 */
public class TcMviewSourceItem {
	public String name;
	public String url;
	
	TcMviewSourceItem(String name, String url) {
		this.name = name;
		this.url = url;
	}
	
	public String toString() {
		return name;
	}
}
//=============================================================================
}
